﻿using System;
using System.Security.Cryptography;
using System.Text;
using Xoohoo.Extensions;

namespace Xoohoo.Utilities.Cryptography
{
    /// <summary>
    /// SHA1加密算法
    /// </summary>
    public static class SHA1
    {
        public static string Encrypt(this string rawString)
        {
            if (rawString.IsNullOrEmpty()) throw new ArgumentNullException("rawString");

            return Convert.ToBase64String(EncryptToByteArray(rawString));
        }
        public static byte[] EncryptToByteArray(this string rawString)
        {
            if (rawString.IsNullOrEmpty()) throw new ArgumentNullException("rawString");

            byte[] salted = Encoding.UTF8.GetBytes(rawString);
            System.Security.Cryptography.SHA1 hasher = new SHA1Managed();
            byte[] hashed = hasher.ComputeHash(salted);
            return hashed;
        }
    }
}
